package com.ruoyi.crm.sale.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.crm.sale.mapper.CrmBusinessMapper;
import com.ruoyi.crm.sale.mapper.CrmContractCheckMapper;
import com.ruoyi.crm.sale.mapper.CrmContractMapper;
import com.ruoyi.crm.sale.mapper.CrmCustomerMapper;
import com.ruoyi.crm.sale.domain.CrmBusiness;
import com.ruoyi.crm.sale.domain.CrmContract;
import com.ruoyi.crm.sale.domain.CrmContractCheck;
import com.ruoyi.crm.sale.domain.CrmCustomer;
import com.ruoyi.crm.sale.service.ICrmContractCheckService;

/**
 * 合同审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-22
 */
@Service
public class CrmContractCheckServiceImpl implements ICrmContractCheckService 
{
    @Autowired
    private CrmContractCheckMapper crmContractCheckMapper;
    @Autowired
    private CrmContractMapper crmContractMapper;
    @Autowired
    private CrmBusinessMapper crmBusinessMapper;
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;

    /**
     * 查询合同审核
     * 
     * @param id 合同审核ID
     * @return 合同审核
     */
    @Override
    public CrmContractCheck selectCrmContractCheckById(Long id)
    {
        return crmContractCheckMapper.selectCrmContractCheckById(id);
    }

    /**
     * 查询合同审核列表
     * 
     * @param crmContractCheck 合同审核
     * @return 合同审核
     */
    @Override
    public List<CrmContractCheck> selectCrmContractCheckList(CrmContractCheck crmContractCheck)
    {
        return crmContractCheckMapper.selectCrmContractCheckList(crmContractCheck);
    }

    /**
     * 新增合同审核
     * 
     * @param crmContractCheck 合同审核
     * @return 结果
     */
    @Override
    public int insertCrmContractCheck(CrmContractCheck crmContractCheck)
    {
    	crmContractCheck.setCreateTime(DateUtils.getNowDate());
    	crmContractCheck.setUpdateTime(DateUtils.getNowDate());
    	crmContractCheck.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    	
    	//更新合同表合同状态
    	CrmContract contract = crmContractMapper.selectCrmContractById(crmContractCheck.getContractId());
    	contract.setUpdateTime(DateUtils.getNowDate());
    	contract.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
		contract.setCheckStatus(crmContractCheck.getCheckStatus());
		crmContractMapper.updateCrmContract(contract);
		
		updateCrmAndBus(contract);
    	
        return crmContractCheckMapper.insertCrmContractCheck(crmContractCheck);
    }

    /**
     * 修改合同审核
     * 
     * @param crmContractCheck 合同审核
     * @return 结果
     */
    @Override
    public int updateCrmContractCheck(CrmContractCheck crmContractCheck)
    {
    	crmContractCheck.setUpdateTime(DateUtils.getNowDate());
    	crmContractCheck.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));

    	
    	CrmContract contract = crmContractMapper.selectCrmContractById(crmContractCheck.getContractId());
    	contract.setUpdateTime(DateUtils.getNowDate());
    	contract.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
		contract.setCheckStatus(crmContractCheck.getCheckStatus());
		crmContractMapper.updateCrmContract(contract);
		
		updateCrmAndBus(contract);
    	
        return crmContractCheckMapper.updateCrmContractCheck(crmContractCheck);
    }
    
    /**
     * 更新客户表、商机表数据
     * @param contract
     */
    public void updateCrmAndBus(CrmContract contract) {
    	CrmBusiness bus = crmBusinessMapper.selectCrmBusinessById(contract.getBusinessId());
		CrmCustomer cus = crmCustomerMapper.selectCrmCustomerById(contract.getCustomerId());
		if(1 == contract.getCheckStatus()) {
			//更新商机表状态为赢单
			bus.setIsEnd(Long.valueOf(1));
			bus.setStatusRemark("已签合同");
			
			//更新客户表成交单数+1
			cus.setDealCount(cus.getDealCount() + 1);
			cus.setLastDealTime(contract.getOrderDate());
			
		}else if(2 == contract.getCheckStatus()) {
			//更新商机表状态为进行中
			bus.setIsEnd(Long.valueOf(0));
			
			//关联合同表查询客户成交单数
			cus.setDealCount(Long.valueOf(crmContractMapper.selectDealCount(contract.getCustomerId())));
		}
		

		bus.setUpdateTime(DateUtils.getNowDate());
		bus.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
		crmBusinessMapper.updateCrmBusiness(bus);

		cus.setUpdateTime(DateUtils.getNowDate());
		cus.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
		crmCustomerMapper.updateCrmCustomer(cus);
    }

    /**
     * 批量删除合同审核
     * 
     * @param ids 需要删除的合同审核ID
     * @return 结果
     */
    @Override
    public int deleteCrmContractCheckByIds(Long[] ids)
    {
        return crmContractCheckMapper.deleteCrmContractCheckByIds(ids);
    }

    /**
     * 删除合同审核信息
     * 
     * @param id 合同审核ID
     * @return 结果
     */
    @Override
    public int deleteCrmContractCheckById(Long id)
    {
        return crmContractCheckMapper.deleteCrmContractCheckById(id);
    }
}
